{extend name="base"/}
{block name="main"}
<style>.form_message {display: block; height: 38px; line-height: 38px; text-align: left;color:#999;}
.formControls .btn {float:left;margin-right:15px;}
.clear{clear:both;}
.form-label {font-weight: bold;}</style>
<article class="cl pd-20">
    <div id="tab_demo" class="HuiTab" style="margin-bottom: 15px; position:relative;">
        <div class="tabBar clearfix">
            <span class="current">一键上传审核</span>
            <span onclick="window.location.href='__CONF_SITE__admin/config/wx_xiao_logs&type=2'">操作日志</span>
        </div>
    </div>
    {if $isfounder==1}
    <div class="form form-horizontal">
        <div class="row cl" style="margin: 0px;">
            <label class="form-label col-xs-4 col-sm-2">微信模板库ID：</label>
            <input type="text" autocomplete="off" value="{$wx_tmpl.value}" onchange="set_wx_tmpl(this.value)" placeholder="微信模板库ID" class="input-text" id="wx_tmpl">
        </div>
    </div>
    {/if}
    <div class="form form-horizontal">
        <div class="row cl" style="margin-top: 20px;">
            <label class="form-label col-xs-4 col-sm-2">申请授权：</label>
            <input class="btn btn-success radius" onclick="window.location.href='__CONF_SITE__admin/config/WeChatOauth'" type="button" value="点击授权">
            <input class="btn btn-success radius" onclick="do_up()" type="button" value="上传">
            <input class="btn btn-success radius" onclick="code()" type="button" value="体验二维码">
            <input class="btn btn-success radius" onclick="tijiao()" type="button" value="提交审核">
        </div>
        <div class="row cl" style="margin: 0px;margin-top: 30px;">
            <label class="form-label col-xs-4 col-sm-2">状态：</label>
            <div class="formControls col-xs-8 col-sm-9" id="wx_append">
                <span id="wx_info">{$authorized}</span>
                <input id="with_wx" class="btn btn-danger radius" style="display: none" onclick="withdraw()" type="button" value="审核撤回">
            </div>
        </div>
    </div>
    <input type="hidden" value="" id="fabu">
</article>
{/block}
{block name="script"}
<script>
    getWxInfo();
    var running = false;
    function do_up() {
        if(running) return;
        running = true;
        $.ajax({
            type : "post",
            url : "{:url('config/WeChatShang')}",
            dataType : "json",
            success : function(data) {
                console.log(data);
                var code = data['errcode'];
                if(code == 0)
                {
                    layer.msg('上传成功!',{icon:1,time:1500},function () {
                        running = false;
                    });
                }
                else
                {
                    layer.msg(data['msg'],{icon:5,time:1500},function () {
                        running = false;
                    });
                }
            }
        });
    }
    function code(){
        layer.open({
            type: 2,
            area: ['250px', '250px'],
            shade:0.4,
            content: "__CONF_SITE__admin/config/code",
            scrollbar:false,
            title: false
        });
    }
    function getWxInfo() {
        $.ajax({
            type : "post",
            url : "{:url('config/receive_appid')}",
            dataType : "json",
            success : function(data) {
                console.log(data);
                if (data['status']==0){
                    if (data['fabu']==0){
                        $("#wx_info").html('小程序发布成功');
                    }
                    if (data['fabu']==1){
                        $("#wx_info").html('小程序审核通过');
                        $("#wx_append").append("<a onclick='fabu()'><input class='btn btn-success radius' type='button' value='全网发布'></a>");
                    }
                    $("#qr_code").css("display","block");
                }
                if (data['status']==1){
                    $("#wx_info").html('小程序审核未通过,'+data['reason']);
                }
                if (data['status']==2){
                    $("#wx_info").html('小程序正在审核');
                    $("#with_wx").css('display','block');
                }
            }
        });
    }
    function fabu() {
        $.ajax({
            type : "post",
            url : "{:url('admin/config/release')}",
            success : function(data) {
                console.log(data);
                if (data['errcode']==0){
                    layer.msg('发布成功!',{icon:1,time:1000},function () {
                        window.location.reload();
                    });
                }
                else {
                    layer.msg('发布失败!',{icon:2,time:1000});
                }
            }
        });
    }
    function withdraw(){
        $.ajax({
            type : "post",
            url : "{:url('config/wx_withdraw')}",
            dataType : "json",
            success : function(data) {
                if (data['errcode']==0){
                    layer.msg('撤回成功!',{icon:1,time:1000},function () {
                        window.location.reload();
                    });
                }
                if (data['errcode']!=0){
                    layer.msg('撤回次数达到上限!',{icon:1,time:1000});
                }
            }
        });
    }
    function set_wx_tmpl(tmpl_id) {
        $.ajax({
            type : "post",
            url : "{:url('config/set_wx_tmpl')}",
            data:{
                'tmpl_id':tmpl_id
            },
            dataType : "json",
            success : function(data) {
                if (data['code']>0){
                    layer.msg('成功!',{icon:1,time:1000},function () {
                        window.location.reload();
                    });
                }
                else {
                    layer.msg('失败!',{icon:5,time:1000});
                }
            }
        });
    }
    function tijiao() {
        $.ajax({
            type : "post",
            url : "{:url('config/tijiaoshenhe')}",
            dataType : "json",
            success : function(data) {
                if (data['errcode']>0){
                    layer.msg('提交审核失败，请查看操作日志!',{icon:5,time:1000});
                }
                else if(data['errcode']==0){
                    layer.msg('提交审核成功!',{icon:1,time:1000});
                }
            }
        });
    }
    function show_msg(msg) {
        layer.msg(msg,{icon:3,time:1500},function () {
            window.location.reload();
        });
    }
</script>
{/block}